package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolClasses;
import com.ruoyi.system.api.domain.SchoolPayrecord;
import com.ruoyi.system.api.domain.SchoolStudent;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SchoolClassesTj;
import com.ruoyi.system.mapper.SchoolPayrecordMapper;
import com.ruoyi.system.mapper.SchoolStudentMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SchoolClassesMapper;
import com.ruoyi.system.service.ISchoolClassesService;

/**
 * 班级信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@Service
public class SchoolClassesServiceImpl implements ISchoolClassesService 
{
    @Autowired
    private SchoolClassesMapper schoolClassesMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SchoolPayrecordMapper schoolPayrecordMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询班级信息
     * 
     * @param id 班级信息ID
     * @return 班级信息
     */
    @Override
    public SchoolClasses selectSchoolClassesById(Long id)
    {
        return schoolClassesMapper.selectSchoolClassesById(id);
    }

    @Override
    public SchoolClasses selectSchoolClassesXsById(Long id)
    {
        SchoolClasses classes = schoolClassesMapper.selectSchoolClassesById(id);
        SchoolStudent query = new SchoolStudent();
        query.setClassesId(id);
        List<SchoolStudent> list = schoolStudentMapper.selectSchoolStudentList(query);
        List<SchoolStudent> showList = new ArrayList<>();
        for (SchoolStudent obj:list) {
            SchoolPayrecord payQuery = new SchoolPayrecord();
            payQuery.setSchoolId(classes.getSchoolId());
            payQuery.setStudentId(obj.getStudentId());
            List<SchoolPayrecord> payList = schoolPayrecordMapper.selectSchoolPayrecordList(payQuery);
            SysUser sysUser = sysUserMapper.selectUserById(obj.getStudentId());
            obj.setXsUsername(sysUser.getUserName());
            obj.setXsPhone(sysUser.getPhonenumber());
            if (null!=payList&&payList.size()>0) {
                BigDecimal remaind = payList.get(0).getRemainder();
                if (remaind.compareTo(classes.getAmount())>=0) {
                    showList.add(obj);
                }
            }
        }
        classes.setStudentList(showList);
        return classes;
    }

    /**
     * 查询班级信息列表
     * 
     * @param schoolClasses 班级信息
     * @return 班级信息
     */
    @Override
    public List<SchoolClasses> selectSchoolClassesList(SchoolClasses schoolClasses)
    {
        return schoolClassesMapper.selectSchoolClassesList(schoolClasses);
    }

    @Override
    public List<SchoolClassesTj> selectSchoolClassesTjList(SchoolClasses schoolClasses)
    {
        return schoolClassesMapper.selectSchoolClassesTjList(schoolClasses);
    }

    @Override
    public List<SchoolClassesTj> selectSchoolClassesHasList(SchoolClasses schoolClasses)
    {

        return schoolClassesMapper.selectSchoolClassesHasList(schoolClasses);
    }

    @Override
    public List<SchoolClassesTj> selectSchoolClassesNoList(SchoolClasses schoolClasses)
    {
        return schoolClassesMapper.selectSchoolClassesNoList(schoolClasses);
    }

    /**
     * 新增班级信息
     * 
     * @param schoolClasses 班级信息
     * @return 结果
     */
    @Override
    public int insertSchoolClasses(SchoolClasses schoolClasses)
    {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolClasses.setUserId(userId);
        schoolClasses.setCreateTime(DateUtils.getNowDate());
        List<SchoolClasses> classList = schoolClassesMapper.selectSchoolClassesList(new SchoolClasses());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClasses obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASS:DATA");
        redisService.setCacheMap("CLASS:DATA", classMap);
        if (null!=schoolClasses.getSchoolId()) {
            redisService.delCacheMap("CLASS:DATA:"+schoolClasses.getSchoolId());
            redisService.setCacheMap("CLASS:DATA:"+schoolClasses.getSchoolId(), classMap);
        }
        return schoolClassesMapper.insertSchoolClasses(schoolClasses);
    }

    /**
     * 修改班级信息
     * 
     * @param schoolClasses 班级信息
     * @return 结果
     */
    @Override
    public int updateSchoolClasses(SchoolClasses schoolClasses)
    {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        schoolClasses.setUserId(userId);
        List<SchoolClasses> classList = schoolClassesMapper.selectSchoolClassesList(new SchoolClasses());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClasses obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASS:DATAB:"+schoolClasses.getId());
        redisService.delCacheMap("CLASS:DATA");
        redisService.setCacheMap("CLASS:DATA", classMap);
        if (null!=schoolClasses.getSchoolId()) {
            redisService.delCacheMap("CLASS:DATA:"+schoolClasses.getSchoolId());
            redisService.setCacheMap("CLASS:DATA:"+schoolClasses.getSchoolId(), classMap);
        }
        return schoolClassesMapper.updateSchoolClasses(schoolClasses);
    }

    @Override
    public void checkCanDelete(Long[] ids)
    {
        for (Long id:ids) {
            SchoolStudent query = new SchoolStudent();
            query.setClassesId(id);
            List<SchoolStudent> studentList = schoolStudentMapper.selectSchoolStudentList(query);
            if (null!=studentList&&studentList.size()>0) {
                throw new CustomException("不允许删除有学生的班级");
            }
        }
    }

    /**
     * 批量删除班级信息
     * 
     * @param ids 需要删除的班级信息ID
     * @return 结果
     */
    @Override
    public int deleteSchoolClassesByIds(Long[] ids)
    {
        List<SchoolClasses> classList = schoolClassesMapper.selectSchoolClassesList(new SchoolClasses());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClasses obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        for (Long id:ids) {
            redisService.delCacheMap("CLASS:DATAB:" + id);
        }
        redisService.delCacheMap("CLASS:DATA");
        if (null!=classList&&classList.size()>0) {
            redisService.delCacheMap("CLASS:DATA:"+classList.get(0).getSchoolId());
        }
        return schoolClassesMapper.deleteSchoolClassesByIds(ids);
    }

    /**
     * 删除班级信息信息
     * 
     * @param id 班级信息ID
     * @return 结果
     */
    @Override
    public int deleteSchoolClassesById(Long id)
    {
        List<SchoolClasses> classList = schoolClassesMapper.selectSchoolClassesList(new SchoolClasses());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClasses obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASS:DATAB:" + id);
        redisService.delCacheMap("CLASS:DATA");
        if (null!=classList&&classList.size()>0) {
            redisService.delCacheMap("CLASS:DATA:"+classList.get(0).getSchoolId());
        }
        return schoolClassesMapper.deleteSchoolClassesById(id);
    }
}
